/**
 * Created by isoft-work 5 on 1/1/2015.
 */

$(document).ready(function(){
//   render
   renderConsultantSize();
//   event
   bindEventConsultantSizePage();
});

function renderConsultantSize() {
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   var gender =  ISS.SESSION.getSession('session_gender');
   if(!useName) {
      ISS.WINDOW.location('/jsp/user/user-login.jsp');
   }
   else {
      renderView(gender);
      getConsultantSizeUser(useName, gender);
   }
}

function getConsultantSizeUser(useName, gender) {
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/consultant/getConsultantSizeUser/' + useName + '/' + gender,
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseInfo = data.responseObject;
         mapConsultantSizeToView(responseInfo);
      }
   });
}

function bindEventConsultantSizePage() {
   ISS.EVENT.bindEvent('#Consultant_Button', 'click', function (e) {
      getConsultantSizeNew();
   });
}

function getConsultantSizeNew() {
   var result = {};
   mapViewToConsultantSize(result);

   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/consultant/getConsultantSize/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(result),
      success: function (data) {
         var responseInfo = data.responseObject;
         mapConsultantSizeToView(responseInfo);
      }
   });
}

function mapConsultantSizeToView(size) {
   var _soDoCoThe = size.soDoCoThe;
   var gender =  ISS.SESSION.getSession('session_gender');

   ISS.DOM.setVal('#input_member_breast', _soDoCoThe.sdNguc);
   ISS.DOM.setVal('#input_member_waist', _soDoCoThe.sdEo);
   ISS.DOM.setVal('#input_member_butt', _soDoCoThe.sdMong);
   ISS.DOM.setVal('#input_member_back', _soDoCoThe.sdDaiLung);
   ISS.DOM.setVal('#input_member_arm', _soDoCoThe.sdDaiTay);
   ISS.DOM.setVal('#input_member_shoulder', _soDoCoThe.sdVai);

   if(gender == 'NAM') {
      ISS.DOM.setVal('#input_member_femur', _soDoCoThe.sdDui);
   }
   else {
      ISS.DOM.setVal('#input_member_leg', _soDoCoThe.sdDaiChan);
   }

   ISS.DOM.text('#size_so_mi', size.sizeAoSoMi[0]);
   ISS.DOM.text('#size_thun', size.sizeAoThun[0]);
   ISS.DOM.text('#size_quan', size.sizeQuan[0]);
}

function mapViewToConsultantSize(result) {
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   var gender =  ISS.SESSION.getSession('session_gender');
   var user = {};

   user.maTV = useName;
   var _soDoCoThe = {};
   _soDoCoThe.sdNguc = ISS.DOM.getVal('#input_member_breast');
   _soDoCoThe.sdEo = ISS.DOM.getVal('#input_member_waist');
   _soDoCoThe.sdMong = ISS.DOM.getVal('#input_member_butt');
   _soDoCoThe.sdDaiLung = ISS.DOM.getVal('#input_member_back');
   _soDoCoThe.sdDaiTay = ISS.DOM.getVal('#input_member_arm');
   _soDoCoThe.sdVai = ISS.DOM.getVal('#input_member_shoulder');
   _soDoCoThe.thongTinThanhVien = user;

   if(gender == 'NAM') {
      _soDoCoThe.sdDui = ISS.DOM.getVal('#input_member_femur');
   }
   else {
      _soDoCoThe.sdDaiChan = ISS.DOM.getVal('#input_member_leg');
   }

   result.soDoCoThe = _soDoCoThe;
   result.gioiTinhTuVan = gender;
}

function renderView(gender) {
   if(gender == 'NAM') {
      ISS.DOM.show('#show_femur');
      ISS.DOM.hide('#show_leg');
   }
   else {
      ISS.DOM.hide('#show_femur');
      ISS.DOM.show('#show_leg');
   }
}
